/**
 * Created by HAIWEI-LC on 2019/10/29
 */
package com.hw.paltfrom.controller.comper;

import com.hw.paltfrom.pojo.HwOaNotice;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.service.comper.OaNoticeService;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.utils.VerifyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping(value = "/notice")
public class OaNoticeController {

    @Autowired
    private OaNoticeService oaNoticeService;

    //添加广告管理
    @PostMapping(value = "/add")
    public Map<String, Object> addOaNotice(HwOaNotice notice) {
        //必填字段公告分类,公告名称,公告内容,创建用户主键,创建用户名
        Boolean verifyParams = VerifyUtil.verifyParams(notice);

        if (!verifyParams) {
            //缺少必填参数则返回参数错误
            return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
        }
        return oaNoticeService.addOaNotice(notice);
    }

    //删除公告,通过id
    @PostMapping(value = "remove")
    public Map<String, Object> deleteOaNotice(String fId) {
        //id传输错误返回参数错误
        if (StringUtils.isBlank(fId)) {
            return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
        }
        return oaNoticeService.removeById(fId);
    }

    //查询公告,支持通过公告名称模糊查询
    @RequestMapping(value = "list")
    public Map<String, Object> findOaNotice(String fNoticetype, String fTitle, int page, int size) {

        if (page <= 0) {
            page = 1;
        }
        if (size == 0) {
            size = 5;
        }

        return oaNoticeService.findOaNotice(fNoticetype, fTitle, page, size);
    }

    //更新浏览次数
    @PostMapping(value = "addcount")
    public Map<String, Object> addcount(HwOaNotice hwOaNotice) {
        if (hwOaNotice.getfId() == null || hwOaNotice.getfModifyuserid() == null ||
                hwOaNotice.getfModifyusername() == null) {
            ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
        }
        return oaNoticeService.addcount(hwOaNotice);
    }

    //查询公告分类
    @RequestMapping("findTypes")
    public Map<String, Object> findTypes(){
        return oaNoticeService.findTypes();
    }

}
